<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %>
<!DOCTYPE html>
<html lang="en">
	<body>
	<div class="modal fade" id="addSubjectModal" tabindex="-1" role="dialog" aria-labelledby="addSubjectModalLabel" aria-hidden="true">
			<div class="modal-dialog">
				<div class="modal-content">
					<div class="modal-header">
						<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
							&times;
						</button>
						<h4 class="model-title" id="addSubjectModalLabel">
							添加科目
						</h4>
					</div>
					<div class="modal-body">
						<form id="addSubjectForm" method="POST">
						
						<label>科目名称:&nbsp;&nbsp;</label><input type="text" name="name" placeholder="请输入科目名称"><br/>
						<label>科目描述:&nbsp;&nbsp;</label><input type="text" name="description" placeholder="请输入科目描述" /><br/>
						<label>科目题数:&nbsp;&nbsp;</label><input type="text" name="count" placeholder="请输入科目题数"><br/>
						<label>总时长:&nbsp;&nbsp;</label><input type="text" name="time" placeholder="请输入总时长"><br/>
						<label>总成绩:&nbsp;&nbsp;</label><input type="text" name="score" placeholder="请输入总成绩"><br/>
						<label>是否马上开始考试:&nbsp;&nbsp;</label><input name="switch-field-1" class="ace ace-switch ace-switch-6 subjectStatus" checked type="checkbox" value="true"><span class="lbl"></span><br/>
						</form>
					</div>
					
					<div class="modal-footer">
						<button type="button" class="btn btn-default saveSubjectBtn" data-dismiss="modal">添加</button>
						<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
					</div>
					
				</div>
			</div>
		
		</div>
		<div class="modal fade" id="SubjectUserModal" tabindex="-1" role="dialog" aria-labelledby="SubjectUserModalLabel" aria-hidden="true">
			<div class="modal-dialog">
				<div class="modal-content">
					<div class="modal-header">
						<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
							&times;
						</button>
						<h4 class="model-title" id="SubjectUserModalLabel">
							修改参与该科目的学生
						</h4>
						<label>科目ID:</label><span class="subjectId"></span>
					</div>
					<div class="modal-body">
						<div id="subjectStudentContent"></div>
					</div>
					
					<div class="modal-footer">
						<button type="button" class="btn btn-default saveSubjectUserBtn" data-dismiss="modal">添加</button>
						<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
					</div>
					
				</div>
			</div>
		
		</div>
		
		<div class="modal fade" id="editSubjectModal" tabindex="-1" role="dialog" aria-labelledby="editSubjectModalLabel" aria-hidden="true">
			<div class="modal-dialog">
				<div class="modal-content">
					<div class="modal-header">
						<button type="button" class="close" data-dismiss="modal" aria-hidden="true">
							&times;
						</button>
						<h4 class="model-title" id="editSubjectModalLabel">
							更改科目
						</h4>
					</div>
					<div class="modal-body">
						<form id="addSubjectForm" method="POST">
						<label>科目ID:</label><span class="subjectId"></span><br/>
						<label>科目名称:&nbsp;&nbsp;</label><input type="text" name="name" placeholder="请输入科目名称"><br/>
						<label>科目描述:&nbsp;&nbsp;</label><input type="text" name="description" placeholder="请输入科目描述" /><br/>
						<label>科目题数:&nbsp;&nbsp;</label><input type="text" name="count" placeholder="请输入科目题数"><br/>
						<label>总时长:&nbsp;&nbsp;</label><input type="text" name="time" placeholder="请输入总时长"><br/>
						<label>总成绩:&nbsp;&nbsp;</label><input type="text" name="score" placeholder="请输入总成绩"><br/>
						<label>考试状态:&nbsp;&nbsp;</label><input name="switch-field-1" class="ace ace-switch ace-switch-6 subjectStatus" type="checkbox"><span class="lbl"></span><br/>
						</form>
					</div>
					
					<div class="modal-footer">
						<button type="button" class="btn btn-default saveEditSubjectBtn" data-dismiss="modal">更改</button>
						<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
					</div>
					
				</div>
			</div>
		
		</div>
														<label class="position-relative">
														<button id="showaddSubjectModalBtn" class="btn btn-primary">
														<i class="ace-icon fa fa-plus align-top bigger-125"></i>
														添加科目信息
														</button>
														</label>
										<table id="sample-table-1" class="table table-striped table-bordered table-hover">
											<thead>
												<tr>
													<th class="center">
													</th>
													<th>ID</th>
													<th>Name</th>
													<th class="hidden-480">Description</th>

													<th>
														<i class="ace-icon fa fa-clock-o bigger-110 hidden-480"></i>
														Count
													</th>
													<th class="hidden-480">Total_time</th>

													<th>Total_score</th>
													<th>Status</th>
													
													<th>Action</th>
												</tr>
											</thead>

											<tbody>
											<c:forEach items="${subjectList }" var="subject">
												<tr>
													<td class="center">
														<label class="position-relative">
															<input type="checkbox" class="ace" />
															<span class="lbl"></span>
														</label>
													</td>

													<td>
														${subject.id }
													</td>
													
													<td>${subject.name }</td>
													<td class="hidden-480">${subject.description }</td>
													<td>${subject.count }</td>

													<td class="hidden-480">
														${subject.time }
													</td>
													<td>
														${subject.score }
									
													</td>
													
													<td>
														<c:if test="${subject.status == 'false'}"><input name="switch-field-1" class="ace ace-switch ace-switch-6 subjectStatusEdit" type="checkbox"><span class="lbl"></span></c:if>
														<c:if test="${subject.status == 'true'}"><input name="switch-field-1" class="ace ace-switch ace-switch-6 subjectStatusEdit" checked="checked" type="checkbox"><span class="lbl"></span></c:if>
													</td>
													
													<td>
															<button class="btn btn-xs btn-info editSubjectBtn">
																	<i class="ace-icon fa fa-pencil bigger-120" >
																	</i>
															</button>
															<button class="btn btn-xs btn-danger deleteSubjectBtn">
																<i class="ace-icon fa fa-trash-o bigger-120"></i>
															</button>
															<button class="btn btn-xs btn-info subject-student-btn"><i class="ace-icon fa fa-users"></i></button>

														<div class="hidden-md hidden-lg">
															<div class="inline position-relative">
																<button class="btn btn-minier btn-primary dropdown-toggle" data-toggle="dropdown" data-position="auto">
																	<i class="ace-icon fa fa-cog icon-only bigger-110"></i>
																</button>

																<ul class="dropdown-menu dropdown-only-icon dropdown-yellow dropdown-menu-right dropdown-caret dropdown-close">
																	<li>
																		<a href="#" class="tooltip-info" data-rel="tooltip" title="View">
																			<span class="blue">
																				<i class="ace-icon fa fa-search-plus bigger-120"></i>
																			</span>
																		</a>
																	</li>

																	<li>
																		<a href="#" class="tooltip-success" data-rel="tooltip" title="Edit">
																			<span class="green">
																				<i class="ace-icon fa fa-pencil-square-o bigger-120"></i>
																			</span>
																		</a>
																	</li>

																	<li>
																		<a href="#" class="tooltip-error" data-rel="tooltip" title="Delete">
																			<span class="red">
																				<i class="ace-icon fa fa-trash-o bigger-120"></i>
																			</span>
																		</a>
																	</li>
																</ul>
															</div>
														</div>
													</td>
												</tr>
												</c:forEach>
											</tbody>
										</table>
			<script type="text/javascript">
				$("#showaddSubjectModalBtn").click(function(){
					$("#addSubjectModal").modal("show");
					});
				$(".subjectStatus").change(function(){
					});
				$(".saveSubjectBtn").click(function(){
					var name = $("#addSubjectModal input[name='name']").val();
					var description = $("#addSubjectModal input[name='description']").val();
					var count = $("#addSubjectModal input[name='count']").val();
					var time = $("#addSubjectModal input[name='time']").val();
					var score = $("#addSubjectModal input[name='score']").val();
					var status = $(".subjectStatus").prop("checked");
					$.ajax({
						type:'POST',
						url:'saveSubject',
						data:{
							name:name,
							description:description,
							count:count,
							time:time,
							score:score,
							status:status,
							},
						dataType:"json",
						success:function(data){
							if(data.success){
								alert("添加成功!");
								window.location.reload();
								}else{
									alert("添加失败!");
									}
							}
						});
					});
				$(".editSubjectBtn").click(function(){
					var subjectId = $(this).parent().siblings().eq(1).text();
					$.ajax({
						type:'POST',
						url:'${pageContext.request.contextPath}/getSubjectById',
						data:{
							subjectId:subjectId
							},
							dataType:"json",
							success:function(data){
								$(".subjectId").text(subjectId);
								$("#editSubjectModal input[name='name']").val(data.name);
	 							$("#editSubjectModal input[name='description']").val(data.description);
	 							$("#editSubjectModal input[name='count']").val(data.count);
	 							$("#editSubjectModal input[name='time']").val(data.time);
	 							$("#editSubjectModal input[name='score']").val(data.score);
	 							$(".subjectStatus").prop("checked",data.status);
								},
						});
					$("#editSubjectModal").modal("show");
					});
				$(".saveEditSubjectBtn").click(function(){
					var id = $("#editSubjectModal .subjectId").text();
					var name = $("#editSubjectModal input[name='name']").val();
					var description = $("#editSubjectModal input[name='description']").val();
					var count = $("#editSubjectModal input[name='count']").val();
					var time = $("#editSubjectModal input[name='time']").val();
					var score = $("#editSubjectModal input[name='score']").val();
					var status = $("#editSubjectModal .subjectStatus").prop("checked");
					$.ajax({
						type:'POST',
						url:'saveEditSubject',
						data:{
							id:id,
							name:name,
							description:description,
							count:count,
							time:time,
							score:score,
							status:status
							},
							dataType:"json",
							success:function(data){
								if(data.success){
									alert("更改成功!");
									window.location.reload();
									}else{
										alert("更改失败!");
										}
								},
						});
					});

				$("#sample-table-1 .subjectStatusEdit").change(function(){
					var status = $(this).prop("checked");
					var subjectId = $(this).parent().siblings().eq(1).text();
					$.ajax({
						type:'POST',
						url:'subjectStatusEdit',
						data:{
							id:subjectId,
							status:status,
							},
						dataType:"json",
						success:function(data){
							if(data.success){
								alert("考试状态更新成功!");
								}else{
									alert("考试状态更新失败!");
									}
							}
						});
					});

				$(".subject-student-btn").click(function(){
					var subjectId = $(this).parent().siblings().eq(1).text();
					$.ajax({
						type:'POST',
						url:'getAllStudent',
						data:{
							id:subjectId,
							},
						dataType:"json",
						success:function(data){
							var htmlString = "";
							var selectedUser = data.selectUser;
							for(var i=0; i<data.allUser.length;i++){
								var user = data.allUser[i];
								var checked = checkSelectedUser(selectedUser,user.id)?' checked="checked" ':'';
								htmlString +='<div class="checkbox">';
								htmlString +='	<label>';
								htmlString += '		<input name="subjectStudents" class="ace ace-checkbox-2" type="checkbox" value="'+user.id+'"'+checked +' />';
								htmlString += ' 	<span class="lbl">' +user.name+ '</span>';
								htmlString += ' 	</laber>';
								htmlString +='    </div>';
								}
							$("#subjectStudentContent").html(htmlString);
							}
						});
					$(".subjectId").text(subjectId);
					$("#SubjectUserModal").modal("show");
					});
				
					
					
				function checkSelectedUser(selectedUser,userid){
					if(selectedUser.indexOf(userid)>=0){
						return true;
						}
					return false;
					}

				$(".saveSubjectUserBtn").click(function(){
					var subjectStudents = [];
					var subjectId = $("#SubjectUserModal .subjectId").text();
					for(var i = 0;i<$('input[name="subjectStudents"]:checked').length;i++){
						subjectStudents[i] = $($('input[name="subjectStudents"]:checked')[i]).val();
						}
					$.ajax({
						type:'POST',
						url:'saveSubjectStudents',
						data:{
							subjectStudents:subjectStudents,
							subjectId:subjectId,
							},
						dataType:"json",
						success:function(data){
							if(data.success){
								alert("更改成功");
								}else{
									alert("更改失败");
									}
							}
						});
					});

				$(".deleteSubjectBtn").click(function(){
					if(confirm("是否确认删除")){
						var subjectId = $(this).parent().siblings().eq(1).text();
						$.ajax({
							type:'POST',
							url:'deleteSubject',
							data:{
								subjectId:subjectId,
								},
							dataType:"json",
							success:function(data){
								if(data.success){
									alert("删除成功");
									window.location.reload();
									}else{
										alert("删除失败");
										}
								}
							});
						}
					});
			</script>
			</body>
			
</html>
